<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    const obj = {
        uname: 'pink',
        age: 18,
        family: {
          baby: '小pink',
          num: ['爸', '妈'],
        },
      }

      function copy(obj) {
        let newobj = new obj.constructor()
        for (let i in obj) {
          if (obj[i] instanceof Object) {
            newobj[i] = copy(obj[i])
          } else {
            newobj[i] = obj[i]
          }
        }
        return newobj
      }
      console.log(copy(obj))
      // 老师教的
      const o = {}
      function deepCopy(newObj, oldObj) {
      for (let k in oldObj) {
        if (oldObj[k] instanceof Array) {
          newObj[k] = []
          deepCopy(newObj[k], oldObj[k])
        } else if (oldObj[k] instanceof Object) {
          newObj[k] = {}
          deepCopy(newObj[k], oldObj[k])
        }
        else {
          newObj[k] = oldObj[k]
        }
      }
    }
    deepCopy(o, obj)
  </script>
</body>
</html>